package qhit.dao.impl;


import qhit.entity.Student;
import qhit.util.BeanHandler;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 *
 * @author 村头老杨头
 * @version 1.0
 * @date 2021/9/27 11:28
 */
public class StudentDaoImpl {
    private String driver = "com.mysql.cj.jdbc.Driver";
    private String url = "jdbc:mysql://127.0.0.1:3306/edu";
    private String username = "root";
    private String password = "123456";
    public Student getStudentById(String sno) throws Exception {
        Student student = new Student();
        //加载驱动
        Class.forName(driver);
        //创建连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //SQL预处理
        String sql = "select * from student where sno=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        //问号填充
        ps.setObject(1,sno);
        //发送SQL语句并返回结果集
        ResultSet rs = ps.executeQuery();
        //解析结果集
        List list = BeanHandler.handler(Student.class, rs);
        if(list.size()>0){
            student = (Student) list.get(0);
        }
        //关闭
        rs.close();
        ps.close();
        conn.close();
        //返回
        return  student;
    }
    public List<Student> getStudentList() throws Exception {
        //加载驱动
        Class.forName(driver);
        //创建连接
        Connection conn = DriverManager.getConnection(url, username, password);
        //SQL预处理
        String sql = "select * from student";
        PreparedStatement ps = conn.prepareStatement(sql);
        //问号填充

        //发送SQL语句并返回结果集
        ResultSet rs = ps.executeQuery();
        //解析结果集
        List list = BeanHandler.handler(Student.class, rs);
        //关闭
        rs.close();
        ps.close();
        conn.close();
        //返回
        return  list;
    }
}
